﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PMG.model;
using PMG.data;
using PMG.data.Tool;
using System.Text;
using System.IO;
using log4net;

public partial class Survey_SurveyList : AdminPage
{
    #region 注入对象
    private DaoQUESTIONNAIRE _DaoQUESTIONNAIRE;
    public DaoQUESTIONNAIRE DaoQUESTIONNAIRE
    {
        get { return _DaoQUESTIONNAIRE; }
        set { _DaoQUESTIONNAIRE = value; }
    }


    #endregion

    #region 分页参数
    private int pageindex = 1;              // 当前页号
    private int pagesize = 10;              // 每页显示的文章数
    private int pagecount                   // 总页数
    {
        get { return Convert.ToInt32(ViewState["pc"]); }
        set { ViewState["pc"] = value; }
    }
    private int recordcount                 // 总记录数
    {
        get { return Convert.ToInt32(ViewState["rc"]); }
        set { ViewState["rc"] = value; }
    }
    private string where
    {
        get { return (Session["whereprod"] == null ? "STATE = 1" : Session["whereprod"].ToString()); }
        set { Session["whereprod"] = value; }
    }                 // 获取条件
    //列名
    private const string ColumnNames
        = "Id,Title,LSTMODDATE,CREATEDATE";
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        //接受页面参数
            GetRequest();
            //参数准备
            PrepareParam();
            //产生分页
            GenPager();
            //绑定
            if (!IsPostBack)
            {
                //已发表
                DataTable dt = DaoQUESTIONNAIRE.GetPagerByStoreProc(ColumnNames, pagesize, pageindex, "LSTMODDATE", true, this.where);
                this.QnList1.DataSource = dt;
                this.QnList1.DataBind();
                //草稿
                IList<QUESTIONNAIRE> qnlist0 = DaoQUESTIONNAIRE.SearchByWhereOrder(" STATE = 0", "CREATEDATE", true);
                this.QnList0.DataSource = qnlist0;
                this.QnList0.DataBind();
            }
    }

    #region 参数准备
    private void PrepareParam()
    {
        //记录总数
        this.recordcount = DaoQUESTIONNAIRE.GetRecordCount(this.where);
        this.pagecount = GetPageCount(this.recordcount, this.pagesize);
    }
    #endregion

    #region 接收页面参数
    private void GetRequest()
    {
        // 页号
        if (Request["p"] != null && Request["p"] != string.Empty)
        {
            try
            {
                this.pageindex = Convert.ToInt32(Request["p"]);
            }
            catch
            {
                ErrorHandle();
            }
        }
        else
        {
            this.pageindex = 1;
        }
    }
    #endregion

    #region 生成分页器
    private void GenPager()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<div class=\"jogger\">");
        if (recordcount > 0 && pagecount > 0 && pageindex > 0)
        {
            // 向前
            if (pageindex == 1)
            {
                sb.AppendLine("<span class=\"disabled\"> << </span>");
                sb.AppendLine("<span class=\"disabled\"> < </span>");
            }
            else
            {
                sb.AppendFormat("<a href=\"SurveyList.aspx?p=1\"> << </a>");
                sb.AppendFormat("<a href=\"SurveyList.aspx?p={0}\"> < </a>", pageindex - 1);
            }
            // 记录已经显示的页号
            int currMax = 0;
            for (int i = pageindex - 5; i < pageindex + 6; i++)
            {
                // 页号不越界
                if (i > 0 && i <= pagecount)
                {
                    if (i != pageindex)
                        sb.AppendFormat("<a href=\"SurveyList.aspx?p={0}\">{0}</a>", i);
                    else
                        sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                    currMax = i;
                }
            }
            // 添加最后的页号
            if (currMax < pagecount)
            {
                sb.AppendLine("...");
                sb.AppendFormat("<a href=\"SurveyList.aspx?p={0}\">{0}</a>", pagecount);
            }
            // 向后
            if (pageindex < pagecount)
            {
                sb.AppendFormat("<a href=\"SurveyList.aspx?p={0}\"> > </a>", pageindex + 1);
                sb.AppendFormat("<a href=\"SurveyList.aspx?p={0}\"> >> </a>", pagecount);
            }
            else
            {
                sb.AppendLine("<span class=\"disabled\"> >> </span>");
                sb.AppendLine("<span class=\"disabled\"> > </span>");
            }
        }
        sb.AppendLine("</div>");
        lt_pager.Text = sb.ToString();
    }
    #endregion
}
